package com.xfcycle.zebrabikegp.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.xfcycle.zebrabikegp.R;
import com.xfcycle.zebrabikegp.assist.BlueToothAssist;
import com.xfcycle.zebrabikegp.constant.ToastConstant;
import com.xfcycle.zebrabikegp.constant.WebConstant;
import com.xfcycle.zebrabikegp.util.HandleDataByBLEUtil;

import java.util.HashMap;
import java.util.Map;

public class MyProfileGenderActivity extends RootActivity {

    private static final String TAG = "MyProfileGender";

    private ImageView iv_my_profile_gender_back;

    private RadioButton rb_my_profile_gender_male;
    private RadioButton rb_my_profile_gender_female;
    private TextView tv_my_profile_gender_save;

    private Bundle bundle;

    private String uid;

    private String currentCheckedGender = "----";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_profile_gender);

        bundle = this.getIntent().getExtras();

        initView();
        initViewListener();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setViewContent();
    }

    /**
     * 初始化组件
     */
    private void initView() {
        iv_my_profile_gender_back = (ImageView) findViewById(R.id.iv_my_profile_gender_back);

        rb_my_profile_gender_male = (RadioButton) findViewById(R.id.rb_my_profile_gender_male);
        rb_my_profile_gender_female = (RadioButton) findViewById(R.id.rb_my_profile_gender_female);

        tv_my_profile_gender_save = (TextView) findViewById(R.id.tv_my_profile_gender_save);
    }

    /**
     * 初始化组件监听
     */
    private void initViewListener() {
        //回退按钮事件监听
        iv_my_profile_gender_back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                closeActivity();
            }
        });

        //保存用户性别点击事件监听
        tv_my_profile_gender_save.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("----".equals(currentCheckedGender)) {
                    Log.e(TAG, "onClick: --------------- 未取到用户性别");
                    return;
                }
                Map<String, String> params = new HashMap<>();
                params.put(WebConstant.CHANGE_GENDER_PATH_KEY, "Male".equals(currentCheckedGender) ? "0" : "1");
                new GetJsonFromWebJob(WebConstant.CHANGE_GENDER_PATH + uid, WebConstant.PUT, params, REQUEST_SIGN_CHANGE_GENDER, MyProfileGenderActivity.this).start();
            }
        });

        //用户性别单选框 男
        rb_my_profile_gender_male.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    rb_my_profile_gender_female.setChecked(false);
                    currentCheckedGender = "Male";
                }
            }
        });

        //用户性别单选框 女
        rb_my_profile_gender_female.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    rb_my_profile_gender_male.setChecked(false);
                    currentCheckedGender = "Female";
                }
            }
        });
    }

    /**
     * 初始化用户性别赋值
     */
    private void setViewContent() {
        uid = bundle.getString("uid");
        currentCheckedGender = bundle.getString("user_gender");
        //设置当前已选择性别
        if ("Male".equals(currentCheckedGender)) {
            rb_my_profile_gender_male.setChecked(true);
        }
        if ("Female".equals(currentCheckedGender)) {
            rb_my_profile_gender_female.setChecked(true);
        }
    }

    /**
     * 处理从网络获取的数据
     *
     * @param webMsg
     */
    @Override
    public void dealDataFromWeb(Message webMsg) {
        super.dealDataFromWeb(webMsg);

        String webResult = webMsg.obj.toString();
        int webSign = webMsg.what;

        //判断缺少参数
        if (webResult.contains("none parameter")) {
            Toast.makeText(this.getApplicationContext(), ToastConstant.NETWORK_FAILURE, Toast.LENGTH_SHORT).show();
            Log.e(TAG, "dealDataFromWeb: ----------------- 缺少参数");
            return;
        }
        //判断用户不存在
        if (webResult.contains("none user")) {
            Toast.makeText(this.getApplicationContext(), ToastConstant.ACCOUNT_NOT_EXIST, Toast.LENGTH_SHORT).show();
            Log.e(TAG, "dealDataFromWeb: ------------- 用户不存在");
            return;
        }

        //修改密码成功，跳转到登录界面
        if (REQUEST_SIGN_CHANGE_GENDER == webSign && "".equals(webResult)) {
            Bundle backBundle = new Bundle();
            backBundle.putString("checkGender",currentCheckedGender);
            setResult(RESULT_OK,new Intent().putExtras(backBundle));
            finish();
        }

        //判断在其他设备登陆
        if (webMsg.obj.toString().contains("you are signed in at another location") && REQUEST_SIGN_LOGOUT != webMsg.what) {
            //断开连接
            HandleDataByBLEUtil.disConnectDevice();
            //改变蓝牙辅助判断类的值
            BlueToothAssist.isConnect = false;
            BlueToothAssist.currentConnectBikeId = null;

            try {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle(ToastConstant.DIALOG_WARNING);
                builder.setMessage(ToastConstant.OTHER_DEVICE_LOGIN);

                builder.setPositiveButton(ToastConstant.DIALOG_I_KNOW, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        //回到登录界面
                        activityChange(LOGIN_ACTIVITY, null, null);
                        HandleDataByBLEUtil.disConnectDevice();
                    }

                });
                //展示提示框
                builder.create().show();
            }catch (Exception e){
                Toast.makeText(this.getApplicationContext(), ToastConstant.OTHER_DEVICE_LOGIN, Toast.LENGTH_LONG).show();
                activityChange(LOGIN_ACTIVITY, null, null);
                HandleDataByBLEUtil.disConnectDevice();
            }
            return;
        }
    }
}
